package main.service.mail.http;


import main.service.mail.bean.MailCountBean;
import main.service.mail.bean.TokenBean;
import main.service.mail.http.main.URLConnection;
import main.service.mail.util.EmailUtils;
import main.service.mail.util.WUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class HttpUtil {
    public static final int MAIL = 1;
    public static final int LOGIN = 2;
    public static final int LOG = 3;
    private static final String corpid = "wmdd1b6c9549b34fde";
    private static final String corpsecretLog = "desVFFYRjYsqvQPgxH7pWZL4exmCtm5IGZFoeQn1xgwuXU324Bpnut6NprWIRwsX";
    private static final String corpsecretMail = "939qtngjmGLG0ITbomkpKPJY8JkegVOp22xzhqHl1IZR0Axb_flr-W9gmquYid8v";
    private static final String corpsecretLogin = "0wUSHjPYE2N9Thg98ierkeZ22xlgq8WwaY2MTJ6LWo73Fcu3IQRmISwxtsQN19nL";

    public static TokenBean getTokenBean(int type) { // 1: mail 2:login
        String corpsecret = "";
        if (type == MAIL)
            corpsecret = corpsecretMail;
        else if (type == LOG)
            corpsecret = corpsecretLog;
        else
            corpsecret = corpsecretLogin;
        String params = "gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
        String http = URLConnection.request(params);
        if (EmailUtils.NEED_SAVE_DIR)
            WUtil.writeStringToFile(http, "gettoken");
        return new Gson().fromJson(http, new TypeToken<TokenBean>() {
        }.getType());
    }

    public static MailCountBean getMailCountBean(String access_token) { // 邮件未读
        String params = "mail/newcount?access_token=" + access_token + "&userid=" + EmailUtils.ACCOUNT;
        String http = URLConnection.request(params);
        if (EmailUtils.NEED_SAVE_DIR)
            WUtil.writeStringToFile(http, "newcount");
        return new Gson().fromJson(http, new TypeToken<MailCountBean>() {
        }.getType());
    }

    public static void getLoginUrl(String access_token) {
        String params = "service/get_login_url?access_token=" + access_token + "&userid=" + EmailUtils.ACCOUNT;
        String http = URLConnection.request(params);
        if (EmailUtils.NEED_SAVE_DIR)
            WUtil.writeStringToFile(http, "get_login_url");
    }

    public static void getLogOperation(String access_token) {
        String params = "log/operation?access_token=" + access_token;
        String json = "{\"type\": 0," +
                "\"begin_date\": \"" + EmailUtils.START_TIME + "\"," +
                "\"end_date\": \"" + "\"" +
                "}";
        String http = URLConnection.sendPost(params, json);
        if (EmailUtils.NEED_SAVE_DIR)
            WUtil.writeStringToFile(http, "operation");
    }

    public static void getLoginLog(String access_token) {
        String params = "log/login?access_token=" + access_token;
        String json = "{" +
                "\"begin_date\": \"" + EmailUtils.START_TIME + "\"," +
                "\"end_date\": \"" + "\"," +
                "\"mailtype\": 1," +
                "\"userid\": \"" + EmailUtils.ACCOUNT + "\"," +
                "\"subject\": \"test\"" +
                "}";
        String http = URLConnection.sendPost(params, json);
        if (EmailUtils.NEED_SAVE_DIR)
            WUtil.writeStringToFile(http, "login");
    }
}
